#include "MainCityProtection.h"
#include "Expand/PluginsManager.h"
#include "Expand/Plugins/AuthPlugin.h"

#include "Expand/EventManager.h"
#include "Blocks/ChestBlock.h"
#include "Blocks/MagnetBlock.h"

#include "PlayerEvent.h"
#include "GameSingleton.h"
#include "Log.h"

#include <fstream>
#include <cfloat>

namespace CXHGT
{
    static const std::string titleText = "MainCityProtection";


    MainCityProtection::MainCityProtection()
    {
        m_playerEvent = new PlayerEvent(this);
        GameSingleton::eventManager->addEvent<IPlayerEvent>(m_playerEvent);
        GameSingleton::eventManager->addEvent<ICreatureEvent>(m_playerEvent);
        GameSingleton::eventManager->addEvent<ISignEvent>(m_playerEvent);
    }

    const std::string &MainCityProtection::getPluginName()
    {
        static const std::string pluginName = "主城保护";
        return pluginName;
    }

    const std::string &MainCityProtection::getIntroduce()
    {
        static const std::string introduce = "\n主城保护";
        return introduce;
    }

    uint32_t MainCityProtection::getUseAuthLevel()
    {
        return 100;
    }

    AuthDisplayType MainCityProtection::getDisplayType()
    {
        return AuthDisplayType::Authority;
    }

    void MainCityProtection::processCmd(ChatObj &chatObj, std::vector<std::string> &cmdDataVec) {}


    bool MainCityProtection::isMainCity(const Point3& point)
    {
        Point2 point2(point.posX >> 8, point.posZ >> 8);
        
        auto it = m_MainCityProtectionSet.find(point2);
        if (it != m_MainCityProtectionSet.end())
        {
            return true;
        }
        return false;
    }

    void MainCityProtection::load()
    {
        authPlugin = GameSingleton::pluginsManager->findPlugin<AuthPlugin>();

        std::ifstream stream;
        stream.open("./Plugins/MainCityProtection/MainCityProtection.json");
        if(stream.is_open())
        {
            nlohmann::json jsonData;
            stream >> jsonData;

            for(auto &item : jsonData["data"])
            {
                m_MainCityProtectionSet.insert(item.get<Point2>());
            }
        }
        else
        {
            m_MainCityProtectionSet.insert(Point2(0,0));
        }
        stream.close();
    }

    void MainCityProtection::save()
    {
        const std::string pluginDataPath = "./Plugins/MainCityProtection";
        if(std::filesystem::exists(pluginDataPath) == false)
        {
            if(std::filesystem::create_directories(pluginDataPath) == false)
            {
                throw std::runtime_error("检测不到起床战争数据文件夹，并且无法创建新的 主城保护 数据文件夹");
            }
        }

        m_mapMutex.lock();

        nlohmann::json jsonData = {{ "data", m_MainCityProtectionSet }};

        m_mapMutex.unlock();

        Utils::SaveTextFile(fmt::format("{}/MainCityProtection.json", pluginDataPath), jsonData.dump());
    }

    void MainCityProtection::getCmds(std::vector<std::string> &cmdVec){}

} // namespace PocketSurvival


using namespace PocketSurvival;

extern "C" void initialize()
{
    GameSingleton::pluginsManager->registerPlugin<CXHGT::MainCityProtection>();
    Log::Info("成功加载 主城保护 插件");
}